﻿#region Copyright Notice
// This file is part of GestionPedago.
// <description>
// Copyright (C) 2012 ENI Ecole Informatique
// 
// GestionPedago is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
// 
// <program> is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
// GNU General Public License for more details.
// 
// You should have received a copy of the GNU General Public License
// along with this program.  If not, see <http://www.gnu.org/licenses/>.
#endregion

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using GestionPedago.EntityContext;
using GestionPedago.EntityContext.EntityOPTools;

namespace GestionPedago.Persistence.OPManager
{
    /// <summary>
    /// Manager de persistance de l'Entity CompetenceParECF
    /// </summary>
    public abstract class CompetenceParECFOPManager
    {
        /// <summary>
        /// Méthode de récupération d'une liste d'objet CompetenceParECF
        /// </summary>
        /// <param name="recherche">Filtre de recherche</param>
        /// <param name="orderByLibelleAsc">Sens du tri (true = ascendant)</param>
        /// <returns></returns>
        public static List<CompetenceParECF> GetListeCompetenceParECF(string recherche, bool orderByLibelleAsc = true)
        {
            try
            {
                IQueryable<CompetenceParECF> query = EntityConnection.GetContext().CompetenceParECF;

                query.Where(it => (recherche == null || recherche.Trim() == "" || it.Competence.Libelle.ToUpper().Contains(recherche.ToUpper())));

                if (orderByLibelleAsc)
                {
                    query.OrderBy(it => it.Competence.Libelle);
                }
                else
                {
                    query.OrderByDescending(it => it.Competence.Libelle);
                }
                query.Select(it => it);

                return query.ToList();
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }

        /// <summary>
        /// Méthode de récupération d'une liste d'objet CompetenceParECF pour un ECF
        /// </summary>
        /// <param name="idECF">Id de l'ECF</param>
        /// <returns></returns>
        public static List<CompetenceParECF> GetListeCompetenceParECFByIdECF(int idECF)
        {
            try
            {
                IQueryable<CompetenceParECF> query = EntityConnection.GetContext().CompetenceParECF;

                query = query.Where(it => it.IdECF == idECF);

                return query.ToList();
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }

        /// <summary>
        /// Méthode de récupération d'un objet CompetenceParECF par son id
        /// </summary>
        /// <param name="id">Identifiant de l'objet CompetenceParECF</param>
        /// <returns>L'objet CompetenceParECF</returns>
        public static CompetenceParECF GetCompetenceParECFById(int id)
        {

            try
            {
                var query = from it in EntityConnection.GetContext().CompetenceParECF
                            where it.IdCompetenceParECF == id
                            select it;

                CompetenceParECF c = null;

                c = query.First();

                return c;

            }
            catch (Exception ex)
            {
                throw ex;
            }

        }

        /// <summary>
        /// Méthode de récupération d'un objet CompetenceParECF par l'idECF et idCompetence
        /// </summary>
        /// <param name="idCompetence">Identifiant de la compétence</param>
        /// <param name="idECF">Identifiant de l'ECF</param>
        /// <returns>L'objet CompetenceParECF</returns>
        public static CompetenceParECF GetCompetenceParECFByIdCompetenceIdECF(int idCompetence, int idECF)
        {

            try
            {
                var query = from it in EntityConnection.GetContext().CompetenceParECF
                            where it.IdCompetence == idCompetence && it.IdECF == idECF
                            select it;

                CompetenceParECF c = null;

                c = query.First();

                return c;

            }
            catch (Exception ex)
            {
                throw ex;
            }

        }

        /// <summary>
        /// Méthode d'ajout d'un objet CompetenceParECF
        /// </summary>
        /// <param name="c">L'objet CompetenceParECF à ajouter</param>
        /// <returns>L'objet CompetenceParECF</returns>
        public static CompetenceParECF AjouterCompetenceParECF(CompetenceParECF c)
        {
            try
            {
                if (!EntityStateChecker.IsDetached(c))
                {
                    EntityConnection.GetContext().CompetenceParECF.Detach(c);
                }
                EntityConnection.GetContext().CompetenceParECF.AddObject(c);
                EntityConnection.GetContext().SaveChanges();
                return c;
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }

        /// <summary>
        /// Méthode de suppression d'un objet CompetenceParECF
        /// </summary>
        /// <param name="c">L'objet CompetenceParECF à supprimer</param>
        public static void SupprimerCompetenceParECF(CompetenceParECF c)
        {
            try
            {
                EntityConnection.GetContext().CompetenceParECF.DeleteObject(c);
                EntityConnection.GetContext().SaveChanges();
            }
            catch (Exception ex)
            {
                throw ex;
            }

        }
    }
}
